package com.fenqing.leetcode.code.t200_300;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.HashSet;
import java.util.Set;

/**
 * @author fenqing
 */
public class Topic202 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic202(String input) {
        super(input);
    }

    @Execute
    public boolean isHappy(int n) {
        Set<Integer> hash = new HashSet<>();
        while (true) {
            if (n == 1) {
                return true;
            }
            n = calculate(n);
            if (hash.contains(n)) {
                return false;
            } else {
                hash.add(n);
            }
        }
    }

    public int calculate(int n){
        int res = 0;
        while (n != 0) {
            int num = n % 10;
            n /= 10;
            res += num * num;
        }
        return res;
    }

    public static void main(String[] args) {
        Topic202 topic202 = new Topic202("2");
        boolean run = topic202.run();
        System.out.println(run);
    }
}
